package com.example.lenovo.demo.presenter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Toast;

import com.example.lenovo.demo.MainActivity;
import com.example.lenovo.demo.model.Model;
import com.example.lenovo.demo.model.databean.HeaderBean;
import com.example.lenovo.demo.model.databean.HomeBean;
import com.example.lenovo.demo.model.idatabean.IModel;
import com.example.lenovo.demo.model.idatabean.OnHeaderSuccess;
import com.example.lenovo.demo.model.idatabean.OnHomeSuccess;
import com.example.lenovo.demo.presenter.adapter.HomeMyRvAdapter;
import com.example.lenovo.demo.presenter.listener.HidingScrollListener;
import com.example.lenovo.demo.view.customview.MyRecyclerView;
import com.example.lenovo.demo.view.ifgview.IHomeFragment;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2017/1/6.
 */

public class HomeFgPresenter implements PullToRefreshBase.OnRefreshListener2{
    private IHomeFragment iHomeFragment;
    private IModel iModel;
    private List<HeaderBean.DataBean> totalData;
    private RecyclerView recyclerView;
    private Context context;
    private List<Object> list;
    private HomeMyRvAdapter adapter;
    private MyRecyclerView myRecyclerView;

    public HomeFgPresenter(IHomeFragment iHomeFragment) {
        this.iHomeFragment = iHomeFragment;
        this.iModel =new Model();
        this.list=new ArrayList<>();
        this.context=iHomeFragment.getFgContext();
        this.myRecyclerView=iHomeFragment.getMyRv();
        this.recyclerView=myRecyclerView.getRefreshableView();
        this.adapter=new HomeMyRvAdapter(context,list,iHomeFragment.getFgManager(),iHomeFragment.getHomeFragment());
        this.totalData=new ArrayList<>();
        list.add(totalData);
    }
    /**
     * 实现控件的数据展示
     * */
    public void initData(){
        //加载
        iHomeFragment.getProgressDialog().show();
        initRecyclerView();

        /**
         * Model层加载实体类数据回调方法
         * */
        iModel.sendHeaderBean(iHomeFragment.getFgContext(), new OnHeaderSuccess() {
            @Override
            public void HeaderSuccess(List<HeaderBean.DataBean> data) {
               list.set(0,data);
                //加载完头部再加载item
                iModel.sendHomeBean(iHomeFragment.getFgContext(), new OnHomeSuccess() {
                    @Override
                    public void HomeSuccess(List<HomeBean.DataBean> data) {
                        list.addAll(data);
                        recyclerView.setAdapter(adapter);
                        iHomeFragment.getProgressDialog().dismiss();
                    }

                    @Override
                    public void HomeFailed(List<HomeBean.DataBean> data) {
                        list.addAll(data);
                        recyclerView.setAdapter(adapter);
                        iHomeFragment.getProgressDialog().dismiss();
                    }
                });
            }

            @Override
            public void HeaderFailed() {
                //加载完头部再加载item
                iModel.sendHomeBean(iHomeFragment.getFgContext(), new OnHomeSuccess() {
                    @Override
                    public void HomeSuccess(List<HomeBean.DataBean> data) {
                        list.addAll(data);
                        recyclerView.setAdapter(adapter);
                        iHomeFragment.getProgressDialog().dismiss();
                    }

                    @Override
                    public void HomeFailed(List<HomeBean.DataBean> data) {
                        list.addAll(data);
                        recyclerView.setAdapter(adapter);
                        iHomeFragment.getProgressDialog().dismiss();
                    }
                });


            }
        });
    }
    /**
     * 初始化RecyclerView
     * */
    private void initRecyclerView() {
        myRecyclerView.setMode(PullToRefreshBase.Mode.BOTH);
        recyclerView.setLayoutManager((new LinearLayoutManager(context)));
        recyclerView.addOnScrollListener(new HidingScrollListener() {
            @Override
            public void onHide() {
                hideViews();
            }

            @Override
            public void onShow() {
                showViews();
            }
        });
    }
    /**
     * 隐藏顶部ToolBar的方法
     * */
    private void hideViews() {
        MainActivity.linearLayout.animate().translationY(- MainActivity.linearLayout.getHeight()/2).setInterpolator(new AccelerateInterpolator(2));
       // MainActivity.drawerLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,MainActivity.drawerLayout.getHeight()-55));
        //  FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mFabButton.getLayoutParams();
        //  int fabBottomMargin = lp.bottomMargin;
        //   mFabButton.animate().translationY(mFabButton.getHeight()+fabBottomMargin).setInterpolator(new AccelerateInterpolator(2)).start();
    }
    /**
     * 显示顶部ToolBar的方法
     * */
    private void showViews() {
       MainActivity.linearLayout.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
        //MainActivity.drawerLayout.setPadding(0,110,0,0);
        //    mFabButton.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
    }

    /**
     * 上拉刷新下拉加载的回调方法
     * */
    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        Toast.makeText(iHomeFragment.getFgContext(),"上拉刷新",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        Toast.makeText(iHomeFragment.getFgContext(),"下拉加载",Toast.LENGTH_SHORT).show();
    }
}
